<?php

    class Podcast {
        
        var $feeds = false;
        
        public function __construct(){
            $this->getPodcastJson();
        }
        
        public function getPodcastJson(){
            $json           = file(PODCAST_JSON);
            $file           = implode($json);
            $data           = json_decode($file,true);
            $feeds          = $data;
            
            foreach($feeds as $f)
                $this->feeds[$f['slug']] = $f;
         }
        
        public function schedulePodcast(){
            
            $now_feed_data = array();    
            foreach($this->feeds as $f){
                if( intval(date('i')) % $f['fetch_cycle']===0 ){
                    $now_feed_data[] = $f;
                }
            }
            
            //
            foreach($now_feed_data as $d){
                $xml = simplexml_load_file($d['url']);
                
                for($i=0; $i<$d['count']; $i++){
                    $item = $xml->channel->item[$i];
                    
                    echo dump($item);
                    
                    $filename = $d['folder']."/".$d['slug'].'-'.md5($item->guid).'.mp3';
                    
                    if(!file_exists($filename)){
                        $file = file_get_contents($item->guid);
                        file_put_contents($filename,$file);
                        echo 'fetched: '.$item->guid."\n";
                    } else {
                        echo 'file exists: '.$item->guid."\n";
                    }
                }            
                
            }
            
        }
        
        public function getFeed($slug){
            return $this->feeds[$slug];
        }
        
        
    }
    
    
?>
